<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;

/**
 * 用户接口控制器
 */
class User extends ApiBase
{
    /**
     * 设置支付密码
     */
    public function setPassword()
    {
        return $this->apiReturn($this->logicUser->setPassword($this->param));
    }
    /**
     * 获得服务中心
     */
    public function sjAdd()
    {
        return $this->apiReturn($this->logicUser->sjAdd($this->param));
    }
	 /**
     * 获得服务中心
     */
    public function getService()
    {        	
        return $this->apiReturn($this->logicUser->getService($this->param));
    }
	 /**
     * 进入小组申请
     */
    public function applyAdd()
    {        	
        return $this->apiReturn($this->logicUser->applyAdd($this->param));
    }
	 /**
     * 编辑头像
     */
    public function headimgUpload()
    {        	
        $data=$this->param;
		$file = request()->file('img');	
		$path=ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'headimg';		
		if($file){
			$info = $file->validate(['size'=>1024*1024*4,'ext'=>'jpg,png,gif,jpeg,webp'])->move($path);
			if($info){
				$str='/upload/extend/headimg/'.date('Ymd').'/'.$info->getFilename(); 
			}else{
				// 上传失败获取错误信息
				return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
			}
		}
		$arr=array('headimg'=>$str);
		if(!empty($data['user_id'])){
			$url=db('user')->where('id',$data['user_id'])->value('headimg');
			if(!empty($url)){
				@unlink($_SERVER['DOCUMENT_ROOT'].$url);
			}			
			db('user')->where('id',$data['user_id'])->update($arr);
		}
		return $this->apiReturn($arr);
    }
	 
     /**
     * 获用户详情
     */
    public function userInfo()
    {        	
        return $this->apiReturn($this->logicUser->getUserInfo($this->param));
    }
	 /**
     * 获用户详情
     */
    public function userEdit()
    {
        $data=$this->param;
        $arr=[];
        if(!empty($data['name'])){
            $arr['name']=$data['name'];
        }
        if(!empty($data['bank'])){
            $arr['bank']=$data['bank'];
        }
        if(!empty($data['bankCode'])){
            $arr['bankCode']=$data['bankCode'];
        }
        if(!empty($data['idcard'])){
            $arr['idcard']=$data['idcard'];
        }
        if(!empty($data['bankName'])){
            $arr['bankName']=$data['bankName'];
        }
        $file = request()->file('wx');
        if($file){
            $info = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg,webp'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'wx');
            if($info){
                $arr['wx']='/upload/wx/'.date('Ymd').'/'.$info->getFilename();
            }else{
                // 上传失败获取错误信息
                return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
            }
        }
        $file1 = request()->file('zfb');
        if($file1){
            $info = $file1->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg,webp'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'zfb');
            if($info){
                $arr['zfb']='/upload/zfb/'.date('Ymd').'/'.$info->getFilename();
            }else{
                // 上传失败获取错误信息
                return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
            }
        }
        db('user')->where('id',$data['user_id'])->update($arr);
        return $this->apiReturn($arr);
    }
	/**
     * 我的分享
     */
    public function serviceList()
    {        	
        return $this->apiReturn($this->logicUser->serviceList($this->param));
    }

	/**
     * 头像名称
     */
    public function headimgName()
    {        	
        return $this->apiReturn($this->logicUser->headimgName($this->param));
    }
	 /**
     * 检查用户
     */
    public function checkUser()
    {        	
        return $this->apiReturn($this->logicUser->checkUser($this->param));
    }
	/**
     * 用户现金明细
     */
    public function moneyyjLog()
    {        	
        return $this->apiReturn($this->logicUser->moneyyjLog($this->param));
    }
	/**
     * 分享收入明细
     */
    public function shareMoneyyj()
    {        	
        return $this->apiReturn($this->logicUser->shareMoneyyj($this->param));
    }

	 /**
     * 二维码
     */
    public function erweima()
    {        	
        return $this->apiReturn($this->logicUser->erweima($this->param));
    }
    /**
     * 二维码背景图
     */
    public function erweimaImg()
    {
        return $this->apiReturn($this->logicUser->erweimaImg($this->param));
    }
	/**
     * 我的分享
     */
    public function tjList()
    {        	
        return $this->apiReturn($this->logicUser->tjList($this->param));
    }

	/**
     * 分享人数及收入
     */
    public function shareNum()
    {        	
        return $this->apiReturn($this->logicUser->shareNum($this->param));
    }
	/**
     * 代理规则
     */
    public function agentConfig()
    {       
        return $this->apiReturn(parse_config_array('agent_config'));
    }
	/**
     * 代理申请
     */
    public function agentApply()
    {        	
        return $this->apiReturn($this->logicUser->agentApply($this->param));
    }
	/**
     * 权限明细记录
     */
    public function logList()
    {        	
        return $this->apiReturn($this->logicUser->logList($this->param));
    }
    
	/**
     * 分享规则
     */
    public function shareConfig()
    {
        $data=$this->param;
        $arr=parse_config_array('shareMoney');
        if(!empty($data['user_id'])){
            $invitation=db('user')->where('id',$data['user_id'])->value('invitation');
            if(empty($invitation)){
                $invitation=initcode();
                db('user')->where('id',$data['user_id'])->update(array('invitation'=>$invitation));
            }
            $arr['invitation']= $invitation;
        }
		return $this->apiReturn($arr);
    }

    /**
     * 意见反馈
     */
    public function suggestAdd()
    {
        $data=$this->param;
        $files = request()->file('image');
        $path=ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'suggest';
        $str='';
        if($files){
            foreach($files as $file) {
                $info = $file->validate(['size' => 1024 * 1024 * 2, 'ext' => 'jpg,png,gif,jpeg,webp'])->move($path);
                if ($info) {
                    $str.= '/upload/extend/suggest/' . date('Ymd') . '/' . $info->getFilename().',';
                } else {
                    // 上传失败获取错误信息
                    return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
                }
            }
        }else{
            $str='';
        }
        $data['imgs']=$str;
        $data['create_time']=time();
        db('suggest')->insert($data);
        return $this->apiReturn();
    }
    /**
     * 举报
     */
    public function reportAdd()
    {
        $data=$this->param;
        $files = request()->file('image');
        $path=ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'report';
        $str='';
        if($files){
            foreach($files as $file) {
                $info = $file->validate(['size' => 1024 * 1024 * 1, 'ext' => 'jpg,png,gif,jpeg,webp'])->move($path);
                if ($info) {
                    $str.= '/upload/extend/report/' . date('Ymd') . '/' . $info->getFilename().',';
                } else {
                    // 上传失败获取错误信息
                    return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
                }
            }
        }else{
            $str='';
        }
        $data['imgs']=$str;
        $data['create_time']=time();
        db('report')->insert($data);
        return $this->apiReturn();
    }
    
}
